﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DevExpress.Drawing.Printing.Internal.DXPageSizeInfo;
using static WinForm_Mes.CheckManagement.CheckWay;

namespace WinForm_Mes.CheckManagement
{
    public partial class CheckItem : DevExpress.XtraEditors.XtraForm
    {

        private int pageIndex = 1;
        private int pageSize = 10;
        public int total;
        public class PageResult<T>
        {
            public int total { get; set; }
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public List<T> items { get; set; }
            public int code { get; set; }
            public string message { get; set; }
            public object data { get; set; }
        }


        public class CheckItemDto
        {
           public string checkItenCode {  get; set; }
            public string checkItemName { get; set; }

            public string classificationName {  get; set; }

        }
        public CheckItem()
        {
            InitializeComponent();
            this.Load += CheckItem_Load;
            checkButton2.Click += checkButton2_Click;

        }
        private async void CheckItem_Load(object sender, EventArgs e)
        {
            检验编号.FieldName = "checkItenCode";
            检验名称.FieldName = "checkItemName";
            分类.FieldName = "classificationName";
            await LoadDataAsync();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        // API调用与数据绑定
        private async Task LoadDataAsync()
        {
            string code = textEdit1.Text.Trim();
            string name = textEdit2.Text.Trim();
            string url = $"http://localhost:5295/api/CheckItem/GetAllCheckItem?PageIndex={pageIndex}&PageSize={pageSize}&CheckItemCode={code}&CheckItemName={name}";
            using (HttpClient client = new HttpClient())
            {
                var result = await client.GetFromJsonAsync<PageResult<CheckItemDto>>(url);
                gridControl1.DataSource = result.items; // 绑定 items 字段
                total = result.total;
            }
            // 可在panelControl2底部加Label显示总条数和分页按钮
        }

        private async void checkButton1_Click(object sender, EventArgs e)
        {
            await LoadDataAsync();
        }
        /// <summary>
        /// 新增事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void checkButton2_Click(object sender, EventArgs e)
        {
            // 创建一个全新的弹窗实例
            using (var addForm = new AddCheckItemForm())
            {
                // 显示弹窗，并等待它的结果
                if (addForm.ShowDialog() == DialogResult.OK)
                {
                    // 只有当弹窗返回OK时，才刷新主列表
                    await LoadDataAsync();
                }
            }
        }

        private void CheckItem_Load_1(object sender, EventArgs e)
        {

        }
    }
}